package org.career.pdung.DAOImpl;

import java.util.List;

import javax.annotation.Resource;

import org.career.pdung.DAO.UserDAO;
import org.career.pdung.domain.Person;
import org.career.pdung.domain.User;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

public class UserDAOImpl implements UserDAO {
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	@SuppressWarnings("unchecked")
	public List<User> getAll() {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("FROM  User");
		return  query.list();
	}
	public User get( Integer id ) {
		Session session = sessionFactory.getCurrentSession();
		User user = (User) session.get(User.class, id);		
		return user;
	}
	public void add(User user) {		
		Session session = sessionFactory.getCurrentSession();
		session.save(user);
	}
	public void delete(Integer id) {		
		Session session = sessionFactory.getCurrentSession();
		Person person = (Person) session.get(Person.class, id);
		session.delete(person);
	}
	public void edit(User user) {		
		Session session = sessionFactory.getCurrentSession();
		User existingUser = (User) session.get(User.class, user.getId());
		existingUser.setFirstName(user.getFirstName());
		existingUser.setLastName(user.getLastName());
		existingUser.setEmail(user.getEmail());
		existingUser.setPassWord(user.getPassWord());
		session.save(existingUser);
	}
	@SuppressWarnings("unchecked")
	public List<User> getUserByEmail(String email) {
		Session	session = sessionFactory.getCurrentSession();
		String sql = "SELECT * FROM user WHERE email = :email";
		SQLQuery query = session.createSQLQuery(sql);
		query.addEntity(User.class);
		query.setParameter("email", email);
		return  query.list();
	}
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		Session	session = sessionFactory.getCurrentSession();
		String sql = "SELECT * FROM user WHERE email = :username";
		SQLQuery query = session.createSQLQuery(sql);
		query.addEntity(User.class);
		query.setParameter("email", username);
		if(query.list().size() > 0 ){
			return (UserDetails) query.list().get(0);
		} else return null;
	}
	public User getUserByFacebookId(String facebookId) {
		Session	session = sessionFactory.getCurrentSession();
		String sql = "SELECT * FROM user WHERE facebookid = :facebookid";
		SQLQuery query = session.createSQLQuery(sql);
		query.addEntity(User.class);
		query.setParameter("facebookid", facebookId);
		if(query.list() != null && query.list().size() > 0){
			return (User) query.list().get(0);
		} else {
			return null;
		}		
	}	
	public User getUserByTwitterId(String twitterId) {
		Session	session = sessionFactory.getCurrentSession();
		String sql = "SELECT * FROM user WHERE twitterid = :twitterid";
		SQLQuery query = session.createSQLQuery(sql);
		query.addEntity(User.class);
		query.setParameter("twitterid", twitterId);
		if(query.list() != null && query.list().size() > 0){
			return (User) query.list().get(0);
		} else {
			return null;
		}		
	}
	public User getUserByLinkedinId(String linkedinId) {
		Session	session = sessionFactory.getCurrentSession();
		String sql = "SELECT * FROM user WHERE linkedinid = :linkedinid";
		SQLQuery query = session.createSQLQuery(sql);
		query.addEntity(User.class);
		query.setParameter("linkedinid", linkedinId);
		if(query.list() != null && query.list().size() > 0){
			return (User) query.list().get(0);
		} else {
			return null;
		}		
	}
}
